package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lh.commonutils.R;
import com.lh.entity.StatisticsDaily;
import com.lh.dao.StatisticsDailyMapper;
import com.lh.feign.CourseFeign;
import com.lh.feign.OrderFeign;
import com.lh.feign.UcenterFeign;
import com.lh.service.IStatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author lh
 * @since 2021-06-09
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements IStatisticsDailyService {

    @Resource
    private UcenterFeign ucenterFeign;
    @Resource
    private StatisticsDailyMapper statisticsDailyMapper;

    @Resource
    private OrderFeign orderFeign;

    @Resource
    private CourseFeign courseFeign;

    /**
     * 统计一天的注册人数
     *
     * @param day
     * @return
     */
    @Override
    public R registerCount(String day) {
        //添加记录前查询是否有相同日期然后删除
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated, day);
        statisticsDailyMapper.delete(wrapper);

        Integer countRegister = ucenterFeign.countRegister(day);
        Integer countOrder = orderFeign.countOrder(day);
        Integer countCourse = courseFeign.countCourse(day);
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setCourseNum(countCourse);
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setOrderNum(countOrder);
        int i = statisticsDailyMapper.insert(statisticsDaily);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    /**
     * 返回图表数据
     *
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> list = statisticsDailyMapper.selectList(wrapper);
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily daily = list.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {

                case "order_num":
                    numList.add(daily.getOrderNum());
                    break;
                case "course_num":
                    numList.add(daily.getCourseNum());
                    break;
                case "register_num":
                    numList.add(daily.getRegisterNum());
                    break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("numList", numList);
        return map;
    }
}
